Skip to content

Snowflake: Support CREATE VIEW myview IF NOT EXISTS #1961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

etgarperets
Copy link
Contributor

…, also added ability to write view name before if not exists in snowflake as it is implemented, replaced dialect of with trait functions

Box::new(BigQueryDialect {}),
]);
let _ = dialects.verified_stmt(sql);
let sql = "CREATE VIEW v IF NOT EXISTS AS SELECT 1";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a quick look I couldn't find which dialects in practice support this syntax? Do you have a link to documentation for one such dialect?

related the MR not super sure it would be necessary to have this behavior covered by a flag and the parser can always look to accept CREATE VIEW IF NOT EXISTS v, as well as the other syntax if its relevant)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I couldn't find official documentation for this syntax, though we have confirmed it works in practice on Snowflake.

I'm happy to remove the dialect flag and update the parser to support both forms for all dialects. Let me know if you'd like me to proceed with that change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah in that case I think we can remove the dialect flags, not sure that we need to support both forms if CREATE VIEW v IF NOT EXISTS is not supported by any dialect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's supported in snowflake, but not officially documented.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah alright, in that case yeah I think we could skip the dialect methods entirely and let the parser accept either format if they show up? Also we can add a comment flagging that the second format is supported by snowflake but is undocumented

…, also added ability to write view name before if not exists in snowflake as it is implemented, replaced dialect of with trait functions
@etgarperets etgarperets force-pushed the “create-view-name-before-if-not-exists” branch from 6f60343 to 46ce27f Compare July 27, 2025 07:55
// Many dialects support `OR ALTER` right after `CREATE`, but we don't (yet).
// ANSI SQL and Postgres support RECURSIVE here, but we don't support it either.
let allow_unquoted_hyphen = dialect_of!(self is BigQueryDialect);
let name = self.parse_object_name(allow_unquoted_hyphen)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it work to only put something like this line after we've parsed? name

let name_before_not_exists = !if_not_exists && self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
let if_not_exists = if_not_exists || name_before_not_exists;

thinking if so that would let us avoid the if/else and mut

let sql: &'static str = "CREATE VIEW IF NOT EXISTS v AS SELECT 1";
let _ = all_dialects().verified_stmt(sql);
let sql = "CREATE VIEW v IF NOT EXISTS AS SELECT 1";
let _ = all_dialects().verified_stmt(sql);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test case for CREATE VIEW IF NOT EXISTS AS SELECT 1 to verify how the parser behaves in that scenario?

src/ast/mod.rs Outdated
@@ -3263,6 +3263,8 @@ pub enum Statement {
materialized: bool,
/// View name
name: ObjectName,
// Name IF NOT EXIST instead of IF NOT EXIST name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Name IF NOT EXIST instead of IF NOT EXIST name
/// If `if_not_exists` is true, this flag is set to true if the view name comes before the `IF NOT EXISTS` clause.
/// Example:
/// ```sql
/// CREATE VIEW myview IF NOT EXISTS AS SELECT 1`
/// ```
/// Otherwise, the flag is set to false if the view name comes after the clause
/// Example:
/// ```sql
/// CREATE VIEW IF NOT EXISTS myview AS SELECT 1`
/// ```

we could say something descriptive like this?

@iffyio iffyio changed the title SGA-11419 Added snowflake ability for if not exists after create view… Snowflake: Support CREATE VIEW myview IF NOT EXISTS Jul 28, 2025
Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @etgarperets!
cc @alamb

@iffyio iffyio merged commit 9127370 into apache:main Jul 30, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants